var app = getApp();
Page({
  data: {
    myPhoto: "",
    photoSetting: false,
    powerInfo: [],
    imgUrl: [
      "", //厂房外景
      "", //办公场所
      "", //常规设施
      "" //营业执照
    ],
    imgId: "",
    message: {},
    imgName: [
      "location_img",
      "place_img",
      "equipment_img",
      "license_img"
    ],
    imageUser: "", //旧图像
    tempfile: "" //新头像
  },
  onLoad: function(e) {
    var openid = wx.getStorageSync("openid");
    var touxiang = wx.getStorageSync("touxiang");
    var that = this;
    wx.request({
      url: 'https://www.mamingwang.cn/api/index/shezhi',
      data: {
        openid: openid
      },
      method: "post",
      success(res) {
        console.log(res)
        //企业能力
        var power = res.data.message.nengli;
        for (var i = 0; i < power.length; i++) {
          for (var j = 0; j < res.data.message.aid.length; j++) {
            if (power[i].aid == res.data.message.aid[j]) {
              power[i].checked = true;
            }
          }
        }
        //公司图片
        //厂房外景
        that.data.imgUrl[0] = res.data.message.location_img;
        //办公场所
        that.data.imgUrl[1] = res.data.message.place_img;
        //常规设施
        that.data.imgUrl[2] = res.data.message.equipment_img;
        //营业执照
        that.data.imgUrl[3] = res.data.message.license_img
        that.setData({
          message: res.data.message,
          powerInfo: power,
          imgUrl: that.data.imgUrl,
          myPhoto: res.data.message.avatars_img, //显示头像
          imageUser: res.data.message.avatars_img, //旧图像
          touxiang: touxiang
        })
      }
    })
  },
  //选择头像
  chooseImgMyPhoto: function() {
    var that = this;
    wx.chooseImage({
      count: 1,
      success: function(res) {
        var myPhoto = res.tempFilePaths[0];
        wx.getFileSystemManager().readFile({
          filePath: res.tempFilePaths[0],
          encoding: "base64",
          success(res) {
            that.setData({
              myPhoto: myPhoto,
              tempfile: res.data
            })
          }
        })
      },
    })
  },
  // 图片
  removeTap: function() {},
  //关闭弹窗
  closeUpLoadPhoto: function() {
    this.setData({
      photoSetting: !this.data.photoSetting
    })
  },
  //弹窗显示
  openUpLoadPhoto: function(e) {
    this.setData({
      photoSetting: !this.data.photoSetting,
      imgId: e.currentTarget.id
    })
  },
  //添加图片
  addPhoto: function() {
    var openid = wx.getStorageSync("openid");
    var that = this;
    if (that.data.imgUrl[that.data.imgId].length == 6) {
      wx.showToast({
        title: '最多只能上传6张图片',
        icon: 'none',
        duration: 2000
      })
    } else {
      wx.chooseImage({
        count: 6,
        success: function(res) {
          for (var i = 0; i < res.tempFilePaths.length; i++) {
            that.data.imgUrl[that.data.imgId].push(res.tempFilePaths[i]);
          }
          that.setData({
            imgUrl: that.data.imgUrl
          })
          var newimg = res.tempFilePaths;
          var images = "";
          var succ = 0;
          for (var a = 0; a < newimg.length; a++) {
            // 图片转码base64
            wx.getFileSystemManager().readFile({
              filePath: newimg[a],
              encoding: "base64",
              success(res) {
                images = images + res.data + ','
                succ++;
                if (succ == newimg.length) {
                  wx.request({
                    url: 'https://www.mamingwang.cn/api/index/Base',
                    data: {
                      image: images,
                      openid: openid,
                      name: that.data.imgName[that.data.imgId],
                    },
                    method: "post",
                    success(res) {
                      wx.request({
                        url: 'https://www.mamingwang.cn/api/index/shezhi',
                        data: {
                          openid: openid
                        },
                        method: "post",
                        success(res) {
                          //厂房外景
                          that.data.imgUrl[0] = res.data.message.location_img;
                          //办公场所
                          that.data.imgUrl[1] = res.data.message.place_img;
                          //常规设施
                          that.data.imgUrl[2] = res.data.message.equipment_img;
                          //营业执照
                          that.data.imgUrl[3] = res.data.message.license_img
                          that.setData({
                            message: res.data.message,
                            imgUrl: that.data.imgUrl,
                          })
                        }
                      })
                    }
                  })
                }
              }
            })
          }
        },
      })
    }
  },
  //删除图片
  delPhoto: function(e) {
    var openid = wx.getStorageSync("openid");
    wx.request({
      url: 'https://www.mamingwang.cn/api/index/file',
      data: {
        openid: openid,
        img: this.data.imgUrl[this.data.imgId][e.currentTarget.id],
        name: this.data.imgName[this.data.imgId]
      },
      method: "post",
      success(res) {
        console.log(res)
      }
    })
    this.data.imgUrl[this.data.imgId].splice(e.currentTarget.id, 1);
    this.setData({
      imgUrl: this.data.imgUrl
    })
  },
  // 返回
  preventTouchMove: function() {},
  goMyPage: function() {
    wx.reLaunch({
      url: "../myID"
    })
  },
  // 多选
  checkAbi: function(e) {
    var powerInfo = this.data.powerInfo;
    var aid = e.currentTarget.id;
    powerInfo[aid - 1].checked = !powerInfo[aid - 1].checked;
    this.setData({
      powerInfo: powerInfo
    })
  },
  // 提交
  formSubmit: function(e) {
    console.log(e)
    var openid = wx.getStorageSync("openid");
    var that = this;
    var powerInfo = this.data.powerInfo;
    var power = "";
    for (var i = 0; i < powerInfo.length; i++) {
      if (powerInfo[i].checked) {
        power = power + powerInfo[i].aid + ","
      }
    };
    var arr = this.data.myPhoto.split(".");
    wx.request({
      url: 'https://www.mamingwang.cn/api/index/shezhi',
      data: {
        sub: "1",
        img: this.data.tempfile, //新头像
        openid: openid,
        suffix: arr[arr.length - 1], //图片后缀名
        uname: e.detail.value.nickName, //名字
        aid: power, //企业能力
        company: e.detail.value.gongsiName, //公司名称
        product: e.detail.value.mainComm, //主营产品
        information: e.detail.value.basicInfo, //基本信息
        ability: e.detail.value.deliver, //交付能力
        address: e.detail.value.address, //经营地址
        introduce: e.detail.value.companyInfo, //公司介绍
        image: this.data.imageUser //旧头像路径
      },
      method: "post",
      success(res) {
        wx.navigateBack({
          detal: 1
        })
      }
    })
  }
})